package com.xinfu.newproject.activity;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xinfu.newproject.R;
import com.xinfu.newproject.adapter.notice_adapter.NoticeListAdapter;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.bean.matter_repair.MatterListDataBean;
import com.xinfu.newproject.ui.notice.NoticeDetailFragment;
import com.xinfu.newproject.interface_.SwichFragmentInterface;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NoticeActivity extends BaseActivity implements SwichFragmentInterface {

    @BindView(R.id.activity_notice_rv)
    RecyclerView mRecyclerView;

    private NoticeListAdapter noticeListAdapter;

    private NoticeDetailFragment noticeDetailFragment;

    private FragmentManager fragmentManager;
    public final static int FRAGMENT_ZERO = 0;
    public final static int FRAGMENT_ONE = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);
        ButterKnife.bind(this);
        registerBack();
        setActivityTitle("通知");
        initViewBind();
        initLisener();

    }

    private void initLisener() {
        List<MatterListDataBean> matterList = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            MatterListDataBean matterListDataBean = new MatterListDataBean();
            matterListDataBean.setTypeName("关于清理楼道内杂物的通知");
            matterListDataBean.setStatus("1");
            matterListDataBean.setTime("2017-07-13");
            matterList.add(matterListDataBean);
        }
        for (int i = 0; i < 2; i++) {
            MatterListDataBean matterListDataBean = new MatterListDataBean();
            matterListDataBean.setTypeName("小区今天停水停电");
            matterListDataBean.setStatus("0");
            matterListDataBean.setTime("2017-07-13");
            matterList.add(matterListDataBean);
        }
        noticeListAdapter.addData(matterList);
    }

    private void initViewBind() {
        fragmentManager = getSupportFragmentManager();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        noticeListAdapter = new NoticeListAdapter();
        mRecyclerView.setAdapter(noticeListAdapter);
    }

    public void showFragment(int index, int type) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        hideFragments(ft);
        //注意这里设置位置
//        position = index;
        switch (index) {
            case FRAGMENT_ZERO:
                break;
            case FRAGMENT_ONE:
                /**
                 * 如果Fragment为空，就新建一个实例
                 * 如果不为空，就将它从栈中显示出来
                 */
                if (noticeDetailFragment == null) {
                    noticeDetailFragment = new NoticeDetailFragment();

                    ft.add(R.id.activity_notice_fl, noticeDetailFragment);

                } else {

                    ft.show(noticeDetailFragment);
                }

                break;

        }
        ft.commitAllowingStateLoss();
    }

    /**
     * 隐藏fragment
     *
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {

        if (noticeDetailFragment != null) {
            transaction.hide(noticeDetailFragment);
        }
//        if (addTemporaryParkFragment != null){
//            transaction.hide(addTemporaryParkFragment);
//        }
    }

    @Override
    public void setSelectedFragment(int fragment, int type) {
        showFragment(fragment, type);
    }

    //解决重叠，方法1
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //如果用以下这种做法则不保存状态，再次进来的话会显示默认的tab
        super.onSaveInstanceState(outState);
    }
}
